﻿namespace Bzure.Mvc.Views
{
    using System.Web.Mvc;

    public class WebFormThemeViewEngine : BuildManagerThemeViewEngine
    {

        public WebFormThemeViewEngine()
            : this(null)
        {
        }

        public WebFormThemeViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            MasterLocationFormats = new[] {
                "~/Themes/{2}/Views/{1}/{0}.master",
                "~/Themes/{2}/Views/Shared/{0}.master",
                 "~/GenericTheme/Views/{1}/{0}.master",
                "~/GenericTheme/Views/Shared/{0}.master"
            };

            AreaMasterLocationFormats = new[] {
                "~/Themes/{3}/Areas/{2}/Views/{1}/{0}.master",
                "~/Themes/{3}/Areas/{2}/Views/Shared/{0}.master",
                  "~/GenericTheme/Areas/{2}/Views/{1}/{0}.master",
                "~/GenericTheme/Areas/{2}/Views/Shared/{0}.master"
            };

            ViewLocationFormats = new[] {
                "~/Themes/{2}/Views/{1}/{0}.aspx",
                "~/Themes/{2}/Views/{1}/{0}.ascx",
                "~/Themes/{2}/Views/Shared/{0}.aspx",
                "~/Themes/{2}/Views/Shared/{0}.ascx",

                 "~/GenericTheme/Views/{1}/{0}.aspx",
                "~/GenericTheme/Views/{1}/{0}.ascx",
                "~/GenericTheme/Views/Shared/{0}.aspx",
                "~/GenericTheme/Views/Shared/{0}.ascx"
            };

            AreaViewLocationFormats = new[] {
                "~/Themes/{3}/Areas/{2}/Views/{1}/{0}.aspx",
                "~/Themes/{3}/Areas/{2}/Views/{1}/{0}.ascx",
                "~/Themes/{3}/Areas/{2}/Views/Shared/{0}.aspx",
                "~/Themes/{3}/Areas/{2}/Views/Shared/{0}.ascx",

                 "~/GenericTheme/Areas/{2}/Views/{1}/{0}.aspx",
                "~/GenericTheme/Areas/{2}/Views/{1}/{0}.ascx",
                "~/GenericTheme/Areas/{2}/Views/Shared/{0}.aspx",
                "~/GenericTheme/Areas/{2}/Views/Shared/{0}.ascx"
            };

            PartialViewLocationFormats = ViewLocationFormats;
            AreaPartialViewLocationFormats = AreaViewLocationFormats;

            FileExtensions = new[] {
                "aspx",
                "ascx",
                "master",
            };
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new WebFormView(controllerContext, partialPath, null, ViewPageActivator);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new WebFormView(controllerContext, viewPath, masterPath, ViewPageActivator);
        }
    }
}
